//
//  HLSquareListViewController.m
//  YueYue
//
//  Created by hulu001 on 2023/5/19.
//

#import "HLSquareListViewController.h"
#import "HLSquareListRequest.h"
#import "HLSquareCell.h"

@interface HLSquareListViewController ()

@property (nonatomic, strong) HLHomeLabelModel *group;

@end

@implementation HLSquareListViewController

- (instancetype)initWithGroup:(HLHomeLabelModel *)group;
{
    self = [super init];
    if (self){
        self.group = group;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    if (!self.request){
        self.request = [[HLSquareListRequest alloc] initWithCode:_group.tagCode];
        self.request.delegate = self;
        [self requestInit];
    }
    [super viewDidAppear:animated];
}


- (void)requestInit
{
    [self showEmptyView];
    self.emptyView.actionButton.enabled = NO;
    [self.emptyView.loadingView startAnimating];
    [self.emptyView setTextLabelText:@"加载中..."];
    [self.emptyView setBackgroundColor:UIColorClear];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.8 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.request start];
    });
}


- (void)initTableView
{
    [super initTableView];
    self.view.backgroundColor = UIColorClear;
    self.tableView.backgroundColor = UIColorClear;
    [self.tableView registerClass:[HLSquareCell class] forCellReuseIdentifier:[HLSquareCell identifyCell]];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.datasources.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    HLSquareCell *cell = [tableView dequeueReusableCellWithIdentifier:[HLSquareCell identifyCell]];
    if (self.datasources.count <= indexPath.row){
        return cell;
    }
    HLSquareModel *model = self.datasources[indexPath.row];
    model.index = indexPath.row;
    cell.model = model;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
}


//- (void)headerRefresh
//{
//    [self.datasources removeAllObjects];
//    HLSquareListRequest *squareRequest = (HLSquareListRequest *)self.request;
//    squareRequest.page = 1;
//    [self.request start];
//}
//
//- (void)footerRefresh
//{
//    HLSquareListRequest *squareRequest = (HLSquareListRequest *)self.request;
//    squareRequest.page += 1;
//    [self.request start];
//}

- (void)requestFinished:(__kindof YTKBaseRequest *)request
{
    [super requestFinished:request];
    
    HLSquareListRequest *squareRequest = (HLSquareListRequest *)request;
    NSArray *squareList = [squareRequest squareList];
    if (squareList.count > 0){
        HLSquareModel *square = [[HLSquareModel alloc] init];
        square.tag = _group;
        HLSquareModel *square1 = [[HLSquareModel alloc] init];
        square1.tag = _group;
        HLSquareModel *square2 = [[HLSquareModel alloc] init];
        square2.tag = _group;
        
        [self.datasources addObjectsFromArray:@[square, square1, square2]];
    }
    if (squareList.count == 0){
        [self.emptyView setLoadingViewHidden:YES];
        self.emptyView.actionButton.enabled = YES;
//        if ([self.group.tagCode isEqualToString:@"default"]){
//            [self.emptyView setImage:UIImageMake(@"empty_icon_5")];
//            [self.emptyView setImageViewInsets:UIEdgeInsetsMake(116, 0, 16, 0)];
//            [self.emptyView setTextLabelText:@"别慌，一点小意外"];
//            [self.emptyView setDetailTextLabelText:@"请确认网络安全后，再次刷新试试"];
//            [self.emptyView setActionButtonTitle:@"刷新"];
//            [self.emptyView setActionButtonTitleColor:UIColorWhite];
//            [self.emptyView.actionButton setBackgroundColor:HLMainColor];
//            [self.emptyView.actionButton setContentEdgeInsets:UIEdgeInsetsMake(10, 50, 10, 50)];
//            [self.emptyView.actionButton setCornerRadius:20];
//            [self.emptyView.actionButton addTarget:self action:@selector(headerRefresh) forControlEvents:UIControlEventTouchUpInside];
//        } else {
            [self.emptyView setImage:UIImageMake(@"empty_icon_6")];
            [self.emptyView setTextLabelText:@"空空如也"];
            [self.emptyView setDetailTextLabelText:[NSString stringWithFormat:@"哇哦，%@页面一个人也没有", _group.tagName]];
            [self.emptyView setActionButtonTitle:@"刷新"];
            [self.emptyView setActionButtonTitleColor:UIColorWhite];
            [self.emptyView.actionButton setBackgroundColor:HLMainColor];
            [self.emptyView.actionButton setContentEdgeInsets:UIEdgeInsetsMake(10, 50, 10, 50)];
            [self.emptyView.actionButton setCornerRadius:20];
            [self.emptyView.actionButton addTarget:self action:@selector(requestInit) forControlEvents:UIControlEventTouchUpInside];
//        }
        
        self.emptyView.textLabelTextColor = UIColorWhite;
    } else {
        [self hideEmptyView];
    }
    
    [self.tableView reloadData];
}

- (void)requestFailed:(__kindof YTKBaseRequest *)request
{
    self.emptyView.actionButton.enabled = YES;
    [super requestFailed:request];
}


#pragma mark - JXCategoryListContentViewDelegate

/**
 实现 <JXCategoryListContentViewDelegate> 协议方法，返回该视图控制器所拥有的「视图」
 */
- (UIView *)listView {
    return self.view;
}
@end
